import axios from 'axios'
import qs from 'qs'

export async function login(user){
    let resp = await axios.post("/api/acb/user/login",qs.stringify({
        username:user.tusername,
        password:user.password
    }));

    console.log(resp);

    let token = resp.headers.authorization;
    if(token){
        localStorage.setItem("token",token);
    }
    return resp;
}
export async function reg(user){
    let resp = await axios.post("/api/acb/user/reg",qs.stringify({
        username:user.tusername,
        password:user.tpassword,
        name:user.tname,
        tel:user.ttel,
        email:user.temail
    }));
    console.log(resp);
    let token = resp.headers.authorization;
    if(token){
        localStorage.setItem("token",token);
    }
    return resp;
}

export async function verify(){

    let token = localStorage.getItem("token");
    if(!token){
        return null;
    }
    let resp = await axios.get("/api/acb/user/verify",{
        headers:{
            'Authorization': 'Bearer ' + token
        }
    });

    return resp;
}

export function loginOut(){
    localStorage.removeItem("token");
}